//
//  MustNewViewController.m
//  ZCooking
//
//  Created by 韩旭 on 16/8/28.
//  Copyright © 2016年 HX_. All rights reserved.
//

#import "MustNewViewController.h"
#import "HX_Macro.h"
#import "HXMustNewCell.h"
#import "HXMustNewModel.h"
#define kCollectionView @"mustNewCell"
#import "MJRefresh.h"
#import "HXMaterialsVC.h"
@interface MustNewViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) UICollectionView *collectionView;
/**
 *  数据源
 */
@property (nonatomic, strong) NSMutableArray *arrAllSource;
@end

@implementation MustNewViewController
- (NSMutableArray *)arrAllSource {
    if (!_arrAllSource) {
        _arrAllSource = [NSMutableArray array];
    }
    return _arrAllSource;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor orangeColor];
    [self getNetWorkingModel];
    [self creatCollectionView];
}
- (void)getNetWorkingModel {
    NSString *url = @"http://api.izhangchu.com/";
    NSString *body = @"page=1&last_id=0&is_marrow=0&methodName=ShequList&size=20&version=4.40&";
    NSDictionary *dicHeader = @{@"Content-Type" : @"application/x-www-form-urlencoded; charset=UTF-8", @"User-Agent" : @"Dalvik/1.6.0 (Linux; U; Android 4.3; R6007 Build/JLS36C)zhangchuAPPforAndroid4.4.0", @"Host" : @"api.izhangchu.com"};
    [HXNetTool postNetWithUrl:url body:body BodyStyle:HXBodyString Header:dicHeader Response:HXJSON Success:^(id result) {
        NSDictionary *results = result;
        NSDictionary *dicData = results[@"data"];
        NSArray *arrData = dicData[@"data"];
        for (NSDictionary *dicAll in arrData) {
            [self.arrAllSource addObject:[HXMustNewModel ModelWithDic:dicAll]];
        }
        [_collectionView reloadData];
    } Failure:^(NSError *error) {
    }];
}

- (void)creatCollectionView {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.scrollDirection = 0;
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 110) collectionViewLayout:layout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.bounces = NO;
    _collectionView.backgroundColor = [UIColor colorWithRed:0.88 green:0.88 blue:0.88 alpha:1.00];
    [self.view addSubview:_collectionView];
    [_collectionView registerNib:[UINib nibWithNibName:@"HXMustNewCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:kCollectionView];
    MJRefreshAutoNormalFooter *foot = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(addData)];
    self.collectionView.mj_footer = foot;
}
- (void)addData {
    static int index = 2;
    NSString *url = @"http://api.izhangchu.com/";
    NSString *body = [NSString stringWithFormat:@"page=%d&token=3C6CB2536B8DE42A4070A68388EA5F99&last_id=0&user_id=1666223&is_marrow=0&methodName=ShequList&size=20&version=4.40&", index];
    NSDictionary *dicHeader = @{@"Content-Type" : @"application/x-www-form-urlencoded; charset=UTF-8", @"User-Agent" : @"Dalvik/1.6.0 (Linux; U; Android 4.3; R6007 Build/JLS36C)zhangchuAPPforAndroid4.4.0", @"Host" : @"api.izhangchu.com"};
    [HXNetTool postNetWithUrl:url body:body BodyStyle:HXBodyString Header:dicHeader Response:HXJSON Success:^(id result) {
        NSDictionary *results = result;
        NSDictionary *dicData = results[@"data"];
        NSArray *arrData = dicData[@"data"];
        for (NSDictionary *dicAll in arrData) {
            [self.arrAllSource addObject:[HXMustNewModel ModelWithDic:dicAll]];
        }
        [_collectionView reloadData];
        [self.collectionView.mj_footer endRefreshing];
    } Failure:^(NSError *error) {
    }];
    index ++;
    
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.arrAllSource.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    HXMustNewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCollectionView forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    cell.mustNewModel = self.arrAllSource[indexPath.row];
    return cell;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    return CGSizeMake(SCREEN_WIDTH / 2, SCREEN_HEIGHT * 0.4);
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section {
    return 0;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    HXMaterialsVC *maters = [[HXMaterialsVC alloc] init];
    HXMustNewModel *newModel = self.arrAllSource[indexPath.row];
    maters.ID = newModel.ID;
    [self.navigationController pushViewController:maters animated:YES];
    
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section {
    return 0;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
